
/**
 * 详情对话框
 */
var InfoDlg = {
    data: {
        isSort: null,
        nodes: []
    }
};

layui.use(['layer', 'admin', 'ax', 'form', 'table', 'treeTable'], function () {

    var $ = layui.$;
    var layer = layui.layer;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var table = layui.table;
    var treeTable = layui.treeTable;

    var Menu = {
        tableId: 'menuTable',
        condition: {
            menuId: "",
            searchWords: "",
            level: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Menu.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'name', width:'24%', title: '菜单名称'},
            {field: 'code', width:'16%', title: '菜单编号'},
            {field: 'pcode', width:'16%', title: '菜单父编号'},
            {field: 'url', width:'18%', title: '请求地址'},
            // {field: 'sort', width:'6%', title: '排序'},
            // {field: 'levels', width:'6%', title: '层级'},
            {field: 'isMenuName', width:'7%', title: '是否菜单'},
            {field: 'statusName', width:'7%', title: '状态', templet:function (d) {
                var str = '<input value="'+d.menuId+'" type="checkbox" name="status" lay-skin="switch" lay-filter="menuStatus" lay-text="启用|禁用">';
                if(d.status == 1){
                    str = '<input value="'+d.menuId+'" type="checkbox" name="status" lay-skin="switch" lay-filter="menuStatus" lay-text="启用|禁用" checked>';
                }
                return str;
            }},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 120}
        ]];
    };

    /**
     * 初始化表格
     */
    Menu.initTable = function (menuId, data) {
        return treeTable.render({
            elem: '#' + menuId,
            url: '/system/menu/listTree',
            where: data,
            page: false,
            height: "full-98",
            cellMinWidth: 100,
            cols: Menu.initColumn(),
            treeColIndex: 1,
            treeSpid: "0",
            treeIdName: 'code',
            treePidName: 'pcode',
            treeDefaultClose: true, //外层关闭
            treeLinkage: false      //内层关闭
        });
    };

    Menu.search = function () {
        var queryData = {};
        queryData['searchWords'] = $("#searchWords").val();
        queryData['level'] = $("#level").val();
        Menu.initTable(Menu.tableId, queryData);
    };

    /**
     * 新增
     */
    Menu.toAdd = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加菜单',
            content: '/system/menu/toAdd',
            area: ['40%', '60%'],
            end: function () {
                //Menu.initTable(Menu.tableId)
                Lu.adminOpenEnd(admin, null);
            }
        })
    };

    /**
     * 点击编辑菜单按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    Menu.onUpdateMenu = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑菜单',
            area: ['40%', '90%'],
            content: Lu.ctxPath + '/system/menu/toUpdate?menuId=' + data.menuId,
            end: function () {
                //Menu.initTable(Menu.tableId)
                Lu.adminOpenEnd(admin, null);
            }
        });
    };

    /**
     * 点击删除菜单按钮
     *
     * @param data 点击按钮时候的行数据
     */
    Menu.onDeleteMenu = function (data) {
        var operation = function () {
            var ajax = new $ax(Lu.ctxPath + "/system/menu/delete", function () {
                Lu.success("删除成功!");
                Menu.condition.menuId = "";
               // Menu.initTable(Menu.tableId);
            }, function (data) {
                Lu.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("menuId", data.menuId);
            ajax.start();
        };
        Lu.confirm("是否删除菜单" + data.name + "?", operation);
    };

    /**
     * 点击删除菜单按钮
     *
     * @param data 点击按钮时候的行数据
     */
    Menu.onSortMenu = function (data) {
        var isSort = encodeURIComponent('parent.InfoDlg.data.isSort');
        var nodes = encodeURIComponent('parent.InfoDlg.data.nodes');
        var treeUrl = encodeURIComponent('/system/menu/menuSortTreeList');
        layer.open({
            type: 2,
            title: '父级菜单',
            area: ['40%', '90%'],
            content: '/system/treeSort?isSort='+isSort+'&nodes='+nodes+'&treeUrl='+treeUrl+'',
            end: function () {
                var isSort = InfoDlg.data.isSort;
                var nodes = InfoDlg.data.nodes;
                if(isSort && nodes && isSort == 1){
                    //排序
                    var ajax = new $ax(Lu.ctxPath + "/system/menu/sort", function () {
                        Lu.success("排序成功!");
                        InfoDlg.data.isSort = null;
                        InfoDlg.data.nodes = [];
                        Menu.condition.menuId = "";
                        Menu.initTable(Menu.tableId);
                    }, function (data) {
                        Lu.error("排序失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("nodes", JSON.stringify(nodes));
                    ajax.start();
                }
            }
        });
    };


    // 渲染表格
    var tableResult = Menu.initTable(Menu.tableId);

    //搜索
    $('#btnSearch').click(function () {
        Menu.search();
    });

    //跳转到添加页面
    $('#btnAdd').click(function () {
        Menu.toAdd();
    });

    //菜单排序
    $('#sort').click(function () {
        Menu.onSortMenu();
    });


    // 全部展开
    $('#expandAll').click(function(){
        treeTable.expandAll('#' + Menu.tableId);
    });
    // 全部关闭
    $('#foldAll').click(function(){
        treeTable.foldAll('#' + Menu.tableId);
    });

    // 工具条点击事件
    table.on('tool(' + Menu.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'update') {
            Menu.onUpdateMenu(data);
        } else if (layEvent === 'delete') {
            Menu.onDeleteMenu(data);
        }
    });

    /**
     * 修改菜单状态
     */
    form.on('switch(menuStatus)', function(data){
        var id = data.value;
        var status = 0;
        if(data.elem.checked){
            status=1;
        }
        var ajax = new $ax(Lu.ctxPath + "/system/menu/updateInfo", function (data) {
            Lu.success("操作成功！");
        }, function (data) {
            Lu.error("操作失败！" + data.responseJSON.message)
        });
        ajax.set("data", JSON.stringify({menuId : id, status : status}));
        ajax.start();
    });


});